﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Web.UI.WebControls;
using Moon.Common.WebInfo;
using System.Data.OleDb;

namespace GPI.Server
{
    public partial class NodeService
    {
        private readonly DAL.NodeDAL dal = new DAL.NodeDAL();

        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            return dal.Exists(Id);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.NodeInfo model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.NodeInfo model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            return dal.Delete(Id);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            return dal.DeleteList(Idlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.NodeInfo GetModel(int Id)
        {

            return dal.GetModel(Id);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中
        /// </summary>
        public Model.NodeInfo GetModelByCache(int Id)
        {

            string CacheKey = "t_nodeModel-" + Id;
            object objModel = Moon.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(Id);
                    if (objModel != null)
                    {
                        int ModelCache = Moon.Common.ConfigHelper.GetConfigInt("ModelCache");
                        Moon.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (Model.NodeInfo)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static List<Model.NodeInfo> GetModelListss(string strWhere)
        {
            DataSet ds = new NodeService().GetList(strWhere);
            return new NodeService().DataTableToList(ds.Tables[0]);
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataTable Hgame(string strWhere)
        {
            DataSet ds = new NodeService().GetList(" id=23");
            return ds.Tables[0];
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public  List<Model.NodeInfo> DataTableToList(DataTable dt)
        {
            List<Model.NodeInfo> modelList = new List<Model.NodeInfo>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Model.NodeInfo model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Model.NodeInfo();
                    if (dt.Rows[n]["Id"] != null && dt.Rows[n]["Id"].ToString() != "")
                    {
                        model.Id = int.Parse(dt.Rows[n]["Id"].ToString());
                    }
                    if (dt.Rows[n]["NodeName"] != null && dt.Rows[n]["NodeName"].ToString() != "")
                    {
                        model.NodeName = dt.Rows[n]["NodeName"].ToString();
                    }
                    if (dt.Rows[n]["ShowTitle"] != null && dt.Rows[n]["ShowTitle"].ToString() != "")
                    {
                        model.ShowTitle = dt.Rows[n]["ShowTitle"].ToString();
                    }
                    if (dt.Rows[n]["NodeImage"] != null && dt.Rows[n]["NodeImage"].ToString() != "")
                    {
                        model.NodeImage = dt.Rows[n]["NodeImage"].ToString();
                    }
                    if (dt.Rows[n]["IsShow"] != null && dt.Rows[n]["IsShow"].ToString() != "")
                    {
                        if ((dt.Rows[n]["IsShow"].ToString() == "1") || (dt.Rows[n]["IsShow"].ToString().ToLower() == "true"))
                        {
                            model.IsShow = true;
                        }
                        else
                        {
                            model.IsShow = false;
                        }
                    }
                    if (dt.Rows[n]["Template"] != null && dt.Rows[n]["Template"].ToString() != "")
                    {
                        model.Template = int.Parse(dt.Rows[n]["Template"].ToString());
                    }
                    if (dt.Rows[n]["ParentID"] != null && dt.Rows[n]["ParentID"].ToString() != "")
                    {
                        model.ParentID = int.Parse(dt.Rows[n]["ParentID"].ToString());
                    }
                    if (dt.Rows[n]["MateKey"] != null && dt.Rows[n]["MateKey"].ToString() != "")
                    {
                        model.MateKey = dt.Rows[n]["MateKey"].ToString();
                    }
                    if (dt.Rows[n]["MateMark"] != null && dt.Rows[n]["MateMark"].ToString() != "")
                    {
                        model.MateMark = dt.Rows[n]["MateMark"].ToString();
                    }
                    if (dt.Rows[n]["Url"] != null && dt.Rows[n]["Url"].ToString() != "")
                    {
                        model.Url = dt.Rows[n]["Url"].ToString();
                    }
                    if (dt.Rows[n]["Content"] != null && dt.Rows[n]["Content"].ToString() != "")
                    {
                        model.Content = dt.Rows[n]["Content"].ToString();
                    }
                    if (dt.Rows[n]["IsHTML"] != null && dt.Rows[n]["IsHTML"].ToString() != "")
                    {
                        if ((dt.Rows[n]["IsHTML"].ToString() == "1") || (dt.Rows[n]["IsHTML"].ToString().ToLower() == "true"))
                        {
                            model.IsHTML = true;
                        }
                        else
                        {
                            model.IsHTML = false;
                        }
                    }
                    if (dt.Rows[n]["HeardSequence"] != null && dt.Rows[n]["HeardSequence"].ToString() != "")
                    {
                        model.HeardSequence = dt.Rows[n]["HeardSequence"].ToString();
                    }
                    if (dt.Rows[n]["IsHeard"] != null && dt.Rows[n]["IsHeard"].ToString() != "")
                    {
                        if ((dt.Rows[n]["IsHeard"].ToString() == "1") || (dt.Rows[n]["IsHeard"].ToString().ToLower() == "true"))
                        {
                            model.IsHeard = true;
                        }
                        else
                        {
                            model.IsHeard = false;
                        }
                    }
                    if (dt.Rows[n]["ClassID"] != null && dt.Rows[n]["ClassID"].ToString() != "")
                    {
                        model.ClassID = int.Parse(dt.Rows[n]["ClassID"].ToString());
                    }
                    if (dt.Rows[n]["OrderBy"] != null && dt.Rows[n]["OrderBy"].ToString() != "")
                    {
                        model.OrderBy = int.Parse(dt.Rows[n]["OrderBy"].ToString());
                    }
                    if (dt.Rows[n]["UpdateDate"] != null && dt.Rows[n]["UpdateDate"].ToString() != "")
                    {
                        model.UpdateDate = DateTime.Parse(dt.Rows[n]["UpdateDate"].ToString());
                    }
                    if (dt.Rows[n]["ModelType"] != null && dt.Rows[n]["ModelType"].ToString() != "")
                    {
                        model.ModelType = dt.Rows[n]["ModelType"].ToString();
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }




        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            return dal.GetRecordCount(strWhere);
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string[] orderby, int startIndex, int endIndex)
        {
            return dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }
        /// <summary>
        /// 获得所有类别的频道
        /// </summary>
        /// <param name="ClassId"></param>
        /// <returns></returns>
        public DataSet GetAllListByClassID(string ClassId)
        {
            return dal.GetList("ClassID=" + ClassId + " order by OrderBy DESC");
        }
        /// <summary>
        /// 获得所有频道
        /// </summary>
        /// <returns></returns>
        public DataSet GetAllNodeByHeard()
        {
            return dal.GetList(" IsHeard=true order by OrderBy DESC");
        }

        /// <summary>
        /// 获得所有栏目和子频道
        /// </summary>
        /// <param name="ClassId"></param>
        /// <returns></returns>
        public DataSet GetAllListByCh(string ParentID)
        {
            return dal.GetList(" ParentID=" + ParentID+" order by OrderBy DESC");
        }

        /// <summary>
        /// 获得所有栏目或子频道
        /// </summary>
        /// <param name="ClassId"></param>
        /// <returns></returns>
        public DataSet GetAllListByCh(string ParentID, bool isTrue)
        {
            return dal.GetList(" IsHeard=" + isTrue + " and ParentID=" + ParentID+" order by OrderBy DESC");
        }


        /// <summary>
        /// 获得所在分类下的Node绑定编号
        /// </summary>
        /// <param name="ClassId"></param>
        /// <returns></returns>

        public TreeNodeCollection GetAllNode(int ClassId)
        {
            TreeNodeCollection node = new TreeNodeCollection();
            List<Model.NodeInfo> list = DataTableToList(dal.GetList(" ParentID=0 and IsHeard=true and ClassID=" + ClassId ).Tables[0]);
            foreach (Model.NodeInfo dr in list)
            {
                TreeNode model = new TreeNode();
                model.Text = dr.NodeName.Length <=5 ? dr.NodeName : dr.NodeName.Substring(0,5)+"...";
                model.Value = dr.Id.ToString();
                CreateTreeViewRecursive(model.ChildNodes, dal.GetList(" IsHeard=true and ParentID=" + dr.Id ).Tables[0]);
                node.Add(model);
            }
            return node;
        }

        /// <summary>
        /// 获得所在分类下的Node绑定处理Action
        /// </summary>
        /// <param name="ClassId"></param>
        /// <returns></returns>

        public TreeNodeCollection GetAllNodeByUrl(int ClassId,string Tag)
        {
            TreeNodeCollection node = new TreeNodeCollection();
            List<Model.NodeInfo> list = DataTableToList(dal.GetList(" ParentID=0 and IsHeard=true and ClassID=" + ClassId).Tables[0]);
            foreach (Model.NodeInfo dr in list)
            {
                TreeNode model = new TreeNode();
                model.Text = dr.NodeName;
                model.NavigateUrl = SystemConfigInfo.TypeMap.FindByText(dr.ModelType) != null ? SystemConfigInfo.TypeMap.FindByText(dr.ModelType).Value : "";
                model.NavigateUrl += dr.Id;
                model.Target = Tag;
                CreateTreeViewRecursiveByUrl(model.ChildNodes, dal.GetList(" IsHeard=true and ParentID=" + dr.Id ).Tables[0], Tag);
                node.Add(model);
            }
            return node;
        }

        /// <summary>
        /// 递归获得子节点
        /// </summary>
        /// <param name="nodes"></param>
        /// <param name="dataSourse"></param>
        private void CreateTreeViewRecursive(TreeNodeCollection nodes, DataTable dataSourse)
        {

            foreach (DataRow dr in dataSourse.Rows)
            {
                TreeNode node = new TreeNode();
                nodes.Add(node);
                string tmp = dr["NodeName"].ToString().Length <= 5 ? dr["NodeName"].ToString() : dr["NodeName"].ToString().Substring(0, 5) + "...";
                node.Text =tmp;
                node.Value = dr["Id"].ToString();
                CreateTreeViewRecursive(node.ChildNodes, dal.GetList(" IsHeard=true and ParentID=" + dr["Id"]).Tables[0]);
            }
        }

        /// <summary>
        /// 递归获得子节点
        /// </summary>
        /// <param name="nodes"></param>
        /// <param name="dataSourse"></param>
        private void CreateTreeViewRecursiveByUrl(TreeNodeCollection nodes, DataTable dataSourse,string Tag)
        {

            foreach (DataRow dr in dataSourse.Rows)
            {
                TreeNode node = new TreeNode();
                nodes.Add(node);
                string tmp = dr["NodeName"].ToString().Length <= 5 ? dr["NodeName"].ToString() : dr["NodeName"].ToString().Substring(0, 5) + "...";
                node.Text = tmp;
                node.NavigateUrl = SystemConfigInfo.TypeMap.FindByText(dr["ModelType"] != null ? dr["ModelType"].ToString() : "") != null ? SystemConfigInfo.TypeMap.FindByText(dr["ModelType"].ToString()).Value : "";
                node.NavigateUrl += dr["Id"];
                node.Target = Tag;
                CreateTreeViewRecursiveByUrl(node.ChildNodes, dal.GetList(" IsHeard=true and ParentID=" + dr["Id"]).Tables[0],Tag);
            }
        }
        #endregion  Method


    }
}
